package com.sls.api.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sls.api.game.pojo.Bill;

/**
 * 使用sax方式解析对账文件
 * 
 * @author cx
 * 
 */
public class SaxParseXml extends DefaultHandler {

	// 存放遍历集合
	private List<Bill> list;
	// 构建Bill对象
	private Bill bill;
	// 用来存放每次遍历后的元素名称(节点名称)
	private String tagName;

	// 只调用一次 初始化list集合
	@Override
	public void startDocument() throws SAXException {
		list = new ArrayList<Bill>();
	}

	// 调用多次 开始解析
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (qName.equals("orderBill")) {
			bill = new Bill();
		}
		this.tagName = qName;
	}

	// 调用多次
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (qName.equals("orderBill")) {
			this.list.add(this.bill);
		}
		this.tagName = null;
	}

	// 只调用一次
	@Override
	public void endDocument() throws SAXException {
	}

	// 调用多次
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (this.tagName != null) {
			//String value = new String(ch, start, length);
			if (this.tagName.equals("ptOrderNo")) {
				// this.bill.setPtOrderNo(value);
			} else if (this.tagName.equals("gameAccount")) {
				//this.bill.setGameAccount(value);
			} else if (this.tagName.equals("goodsId")) {
				//this.bill.setGoodsId(value);
			} else if (this.tagName.equals("status")) {
				//this.bill.setStatus(value);
			} else if (this.tagName.equals("totalPrice")) {
				// this.bill.setTotalPrice(value);
			} else if (this.tagName.equals("unitPrice")) {
				// this.bill.setUnitPrice(value);
			} else if (this.tagName.equals("buyNum")) {
				// this.bill.setBuyNum(value);
			}
		}
	}

	/**
	 * 获取账单文件集合
	 * 
	 * @param date
	 * @param userNo
	 * @return
	 * @throws Exception
	 */
	public static List<Bill> getBills(String path, String date, String userNo)
			throws Exception {
		SAXParser parser = null;
		try {
			// 构建SAXParser
			parser = SAXParserFactory.newInstance().newSAXParser();
			// 实例化 DefaultHandler对象
			SaxParseXml parseXml = new SaxParseXml();
			File file = new File(path);
			// 调用parse()方法
			parser.parse(file, parseXml);
			// 遍历结果
			List<Bill> list = parseXml.getList();
			return list;
		} catch (Exception e) {
			throw e;
		}
	}

	public List<Bill> getList() {
		return list;
	}

	public void setList(List<Bill> list) {
		this.list = list;
	}

	public Bill getBill() {
		return bill;
	}

	public void setBill(Bill bill) {
		this.bill = bill;
	}

	public String getTagName() {
		return tagName;
	}

	public void setTagName(String tagName) {
		this.tagName = tagName;
	}
}
